#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#                                Main validation wraper                                   ++
#                         Lei Zhu (leizhu@fas.harvard.edu), 11/08/2019                    ++
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#///////////////////////////////////////////////////////////////////////////////////////////
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Set working directory, i.e, the path to this platform
#///////////////////////////////////////////////////////////////////////////////////////////
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

setwd("/home/leizhu/platform2")

#///////////////////////////////////////////////////////////////////////////////////////////
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# Load libraies, functions, and control files
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#///////////////////////////////////////////////////////////////////////////////////////////

library(fields); library(rhdf5); library(SDMTools); library(lubridate)
source("control.R")
source("functions.R")

#///////////////////////////////////////////////////////////////////////////////////////////
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#                              Load ATom-1 and 2 RData                                    ++
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#///////////////////////////////////////////////////////////////////////////////////////////

RData_name3	             <- paste(RData_folder,"C11_ATom-1_profile.RData",sep="")
load(RData_name3)
Profile_Obs_mean_P_ATom1 <- Profile_Obs_mean_P
Profile_Obs_mean_T_ATom1 <- Profile_Obs_mean_T
Profile_Obs_mean_ATom1   <- Profile_Obs_mean[,1]

RData_name4	             <- paste(RData_folder,"C12_ATom-2_profile.RData",sep="")
load(RData_name4)
Profile_Obs_mean_P_ATom2 <- Profile_Obs_mean_P
Profile_Obs_mean_T_ATom2 <- Profile_Obs_mean_T
Profile_Obs_mean_ATom2   <- Profile_Obs_mean[,1]

#///////////////////////////////////////////////////////////////////////////////////////////
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#                           Read 1976 US Standard Atmosphere                              ++
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#///////////////////////////////////////////////////////////////////////////////////////////

Standard_Air             <- read.table("1976_US_air.dat",header=T)
Standard_Air_H           <- Standard_Air$alt        # km
Standard_Air_P           <- Standard_Air$press/100  # hPa

#///////////////////////////////////////////////////////////////////////////////////////////
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#                               Read Campaign information                                 ++
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#///////////////////////////////////////////////////////////////////////////////////////////

Campaign_info            <- read.table("campaigns.dat", header=T)

#///////////////////////////////////////////////////////////////////////////////////////////
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#                            Process validation for each campigan                         ++
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#///////////////////////////////////////////////////////////////////////////////////////////

# Open the file to validation results
cat("Validation results by campaigns", file=logFile, append=FALSE, sep = "\n")

# Loop campigans
for(i in 1:length(Campigans_used)){
  Do_validation(Campigans_used[i])
}

